from flask import Flask, request,session
from flask_cors import CORS
from setGlobalConfig import globalSetting
from manageProjecrt import newProjecct
from setConfig import setting
from runProject import project
from reportView import report
from user import user
from Logger import logger
from machineManagement import machineManagement
from helpCenter import userhelp
from userManagement import userManagement
from taskStatistics import taskStatistics
from buildTask import buildTask
from auth import token_required  # 导入认证装饰器
import sys
import signal
import atexit

app = Flask(__name__)
app.secret_key = "ckj.1029"
CORS(app, resources=r'/*', supports_credentials=True)

app.register_blueprint(setting)
app.register_blueprint(newProjecct)
app.register_blueprint(project)
app.register_blueprint(globalSetting)
app.register_blueprint(user)
app.register_blueprint(machineManagement)
app.register_blueprint(report)
app.register_blueprint(userhelp)
app.register_blueprint(userManagement)
app.register_blueprint(taskStatistics)
app.register_blueprint(buildTask)
# 定义不需要认证的路由路径
EXEMPT_ROUTES = [
    '/login',
    '/register',
    '/getAsyncRoutes',
    '/refreshToken',
    '/checkRun',
    '/logout',
    '/file/upload',
    '/file/uploadJmx',
    '/',  # 根路径
    # 可以根据需要添加更多无需认证的路由
]

@app.before_request
def before_request():
    username = session.get('loginname', None)
    client_ip = request.headers.get('X-Real-IP') or request.headers.get('X-Forwarded-For') or request.remote_addr
    # 在每个请求开始时添加日志
    logger.info("%s Processing a new request.", username)

    # 如果请求路径在免认证列表中，则跳过认证
    if request.path in EXEMPT_ROUTES:
        return

    # 应用认证装饰器
    decorator = token_required
    return decorator(lambda: None)()  # 这里暂时返回 None，如果认证通过，则继续执行

@app.route('/')
def hello_world():
    return 'welcome！欢迎使用Solo派。'


if __name__ == '__main__':
    app.run()
